﻿using FluentValidation;
using HRSolutionSystem.Application.Contracts.Persistence;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRSolutionSystem.Application.Features.LeaveType.Commands.CreateLeaveType;

public class CreateLeaveTypeCommandValidator:AbstractValidator<CreateLeaveTypeCommand>
{
    private readonly ILeaveTypeRepository _leaveTypeRepository;

    public CreateLeaveTypeCommandValidator(ILeaveTypeRepository leaveTypeRepository)
	{
		RuleFor(p => p.Name)
			.NotEmpty().WithMessage("{PropertyName} is required")
			.NotNull()
			.MaximumLength(70).WithMessage("{PropertyName} must be fewer than 70 characters");

		RuleFor(p => p.DefaultDays)
			.GreaterThan(100).WithMessage("{PropertyName} cannot be exceed 100")
			.LessThan(1).WithMessage("{PropertyName} cannot be less than 1");

		RuleFor(q => q)
			.MustAsync(LeaveTypeNameUnique).WithMessage("Leave type already exists");

        this._leaveTypeRepository = leaveTypeRepository;
    }

	private async Task<bool> LeaveTypeNameUnique(CreateLeaveTypeCommand command, CancellationToken token)
	{
		return await _leaveTypeRepository.IsLeaveTypeUnique(command.Name);
	}
} 
